//
//  GRXPictureView.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/1.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXPictureView.h"
#import "GRXImageCell.h"
@interface GRXPictureView ()<UICollectionViewDataSource,UICollectionViewDelegate>

/**图片数组*/
@property (nonatomic,strong)NSArray *images;

/**标题数组*/
@property (nonatomic,strong)NSArray *titles;


@end

@implementation GRXPictureView

static NSString * const pictureCell = @"pictureCell";



- (NSArray *)images{
    if (!_images) {
        _images = [NSArray array];
        _images = @[@"picture-1",@"picture-2",@"picture-3",@"picture-4",@"picture-5",@"picture-6",@"picture-7",@"picture-8"];
    }
    return _images;
}

- (NSArray *)titles{
    if (!_titles) {
        _titles = [NSArray array];
        _titles = @[@"李易峰专区",@"2016星运辣汤",@"新年新自拍",@"红人一周穿搭志",@"新年到!",@"大衣就要这样穿",@"时髦学生妹造型",@"男票陪我跨年"];
    }
    return _titles;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpcollectionView];
    }
    return self;
}

- (void)setUpcollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(85, 85);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.bounces = NO;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
    
    
//    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:pictureCell];
    [collectionView registerNib:[UINib nibWithNibName:@"GRXImageCell" bundle:nil] forCellWithReuseIdentifier:pictureCell];
}


- (void)layoutSubviews{
    UICollectionView *collectionView = self.subviews.firstObject;
    collectionView.frame = CGRectMake(0, 0, self.width, self.height);
}


#pragma mark ---数据源
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GRXImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:pictureCell forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.images[indexPath.item]];
    cell.titleLabel.text = self.titles[indexPath.item];

    return cell;
    
}

//图片选择的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//        NSLog(@"%zd",indexPath.item);
}


@end
